﻿using CRMExam.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using CRMExam.Models;
using CRMExam.Helpers;

namespace CRMExam.Controllers
{
    [Authorize]
    public class CompanyController : Controller
    {
        CRMExam1Entities db = new CRMExam1Entities();

        public ActionResult Show()
        {

            var currentUserId = User.Identity.GetUserId();
            AspNetUser currentUser = db.AspNetUsers.FirstOrDefault(u => u.Id == currentUserId);

            var model = db.Companies.Where(c => c.CustomerId == currentUser.CustomerId)
                .Select(x => new CompanyViewModel
                {
                    Id = x.Id,
                    Email = x.Email,
                    CustomerId = x.CustomerId,
                    Name = x.Name,
                    City = x.City,
                    Country = x.Country,
                    ZipCode = x.ZipCode,
                    Phone = x.Phone,
                    StreetName = x.StreetName
                });
            return View(model);
        }

        public ActionResult Details(Guid id)
        {
            string message = "";



            if (id == Guid.Empty)
                message = "Your changes have been updated!";

            var currentUserId = User.Identity.GetUserId();

            AspNetUser currentUser = db.AspNetUsers.FirstOrDefault(u => u.Id == currentUserId);

            CompanyViewModel model = new CompanyViewModel();

            Company company = db.Companies.FirstOrDefault(c => c.CustomerId == currentUser.CustomerId);

            model.Id = company.Id;
            model.Email = company.Email;
            model.Country = company.Country;
            model.City = company.City;
            model.Name = company.Name;
            model.Phone = company.Phone;
            model.StreetName = company.StreetName;
            model.ZipCode = company.ZipCode;
            model.Message = message;

            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Details(CompanyViewModel model)
        {
            var currentUserId = User.Identity.GetUserId();

            AspNetUser currentUser = db.AspNetUsers.FirstOrDefault(u => u.Id == currentUserId);

            Company company = db.Companies.FirstOrDefault(c => c.CustomerId == currentUser.CustomerId);

            company.Id = model.Id;
            company.Email = model.Email;
            company.Country = model.Country;
            company.City = model.City;
            company.Name = model.Name;
            company.Phone = model.Phone;
            company.StreetName = model.StreetName;
            company.ZipCode = model.ZipCode;

            db.SaveChanges();

            return RedirectToAction("Details", model.Id).Success("Huge success!");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}